package org.example;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class XmSeerStar {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\聊天\\qq");
        String name = "XM Seer脱机日常 ver 2.0.61.exe";
        //账户
        String account = "D:\\浏览器下载\\账户.txt";
        Begin(file, name, account);

    }

    //开始重聚
    public static void Begin(File file, String name, String account) throws Exception {
        Map<String,String> map = searchAccount(account);
        for (Map.Entry<String,String> entry:map.entrySet()){
            String names=entry.getKey();
            String password= entry.getValue();
            try {
                searchfile(file, name);
                Thread.sleep(1000);

            } catch (Exception e) {
                e.printStackTrace();
            }
            Thread.sleep(3000);
            login(names, password);
        }
    }
    //登录
    public static void login(String name, String password) throws Exception {
        Robot robot = new Robot();

        // 3. 移动鼠标到“账号输入框”并点击（需提前测试坐标）
        robot.mouseMove(450, 180); // x=300, y=200（根据实际调整）
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        // 全选：Ctrl + A
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        Thread.sleep(100);
        // 删除：Delete
        robot.keyPress(KeyEvent.VK_DELETE);
        robot.keyRelease(KeyEvent.VK_DELETE);
        Thread.sleep(100);
        // 4. 输入账号
        type(robot, name);


        // 5. 按 Tab 或点击密码框（或直接移动）
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);
        Thread.sleep(300);
        // 全选：Ctrl + A
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        Thread.sleep(100);
        // 删除：Delete
        robot.keyPress(KeyEvent.VK_DELETE);
        robot.keyRelease(KeyEvent.VK_DELETE);
        Thread.sleep(100);
        // 6. 输入密码
        type(robot, password);

        // 7. 按 Tab 到登录按钮 或 直接点击登录按钮
        robot.mouseMove(450, 250); // 登录按钮坐标
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        System.out.println("登录操作已发送。");
        Thread.sleep(6000);
        // 7. 一键日常
        robot.mouseMove(1150, 660); // 登录按钮坐标
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

        System.out.println("一键日常已发送。");
    }




    // 模拟键盘输入（逐字符，避免输入法问题）
    private static void type(Robot robot, String text) {
        for (char c : text.toCharArray()) {
            int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
            if (KeyEvent.CHAR_UNDEFINED == keyCode) {
                continue;
            }
            boolean isShift = Character.isUpperCase(c);
            if (isShift) {
                robot.keyPress(KeyEvent.VK_SHIFT);
            }
            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);
            if (isShift) {
                robot.keyRelease(KeyEvent.VK_SHIFT);
            }
            try { Thread.sleep(50); } catch (InterruptedException e) {}
        }
    }

    //递归查找
    public static void searchfile(File file, String name) throws Exception {
        Robot robot = new Robot();
        if (file==null||!file.exists()||file.isFile()){
            return;
        }else if (file.isDirectory()){

            File[] files = file.listFiles();
            if (files!=null&&files.length>0){
                for (File f:files){
                    if (f.isFile()){
                        if (f.getName().contains(name)){
                            System.out.println("找到"+name+"在"+f.getAbsolutePath());
                            Runtime runtime = Runtime.getRuntime();
                            runtime.exec(f.getAbsolutePath());
                            Thread.sleep(3000);
                            robot.mouseMove(950, 520); // x=950, y=520（根据实际调整）
                            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                            Thread.sleep(4000);
                            System.out.println("进入脱机操作已发送。");


                        }
                    }
                    else {
                        searchfile(f,name);
                    }
                }
            }
        }
    }

    //查找账户文件并且读取文件内容
    public static Map<String,String> searchAccount(String acount) throws IOException {
        Map<String,String>  map = new HashMap<>();
        try(BufferedReader br = new BufferedReader(new FileReader(acount));) {

            String line;
            int i = 0;

            //map存储账号和密码
            while ((line = br.readLine()) != null) {

                String[]  arr = line.split("，|,");

                map.put(arr[i],arr[i+1]);

            }

            return map;

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}




